package com.hlcl.huaji;

import android.os.Bundle;
import android.view.View;

import com.elcl.activity.BaseActivity;
import com.elcl.network.AppContext;
import com.elcl.util.StringUtils;
import com.elcl.util.viewutils.ViewUtils;
import com.elcl.util.viewutils.WindowUtils;
import com.elcl.widget.comp.toast.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hlcl.huaji.model.LoginResponse;
import com.hlcl.huaji.model.LoginResult;
import com.hlcl.huaji.server.NetCode;
import com.hlcl.huaji.server.OrderServer;
import com.hlcl.huaji.view.HomeActivity;

/**
 * Created by lichunlin on 2018/2/27.
 * 登录界面
 */

public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_login);

        initBaseView();
        WindowUtils windowUtils = new WindowUtils();
        windowUtils.fullScreen(LoginActivity.this);
    }

    //获取基本的VIew
    private void initBaseView() {
        setListener(R.id.tv_login, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }

    private String[] prompts = new String[]{"请输入您的账号", "请输入您的密码"};
    private OrderServer orderServer = new OrderServer();

    private void login() {
        String[] inputContent = ViewUtils.isEdtNull(this, prompts, R.id.edt_login, R.id.edt_pwd);
        if (null != inputContent) {
            orderServer.loginServer(netHandler, inputContent[0], inputContent[1]);
        }
    }

    private Gson gson = new Gson();

    @Override
    protected void praseJson(int reqId, String json) {
        if (reqId == NetCode.ID_LOGIN) {//登录接口
            if (StringUtils.isNotBlank(json)){
                if (StringUtils.isNotBlank(json)) {
                    LoginResult loginResult = gson.fromJson(json, new TypeToken<LoginResult>() {
                    }.getType());
                    if (null != loginResult) {
                        if (loginResult.getjData().equals("")){//如果为空的话，提示
                            ToastUtils.showToast("账号或密码不正确");
                        }else{
                            LoginResponse loginResponse = loginResult.getjData();
                            if (null != loginResponse) {
                                AppContext.setUid(loginResponse.getUserId());
                                AppContext.setUserInfo(json);
                            }
                            openActivty(HomeActivity.class);
                            finish();
                        }
                    }
                }
            }else{
                ToastUtils.showToast("账号或密码不正确");
            }
        }
    }

}
